<template>
	<view class="index-body">
		<view class="content">
			<goods v-if="index === 0" :act="index"></goods>
			<store v-if="index === 1"></store>
			<index v-if="index === 2" :goods="goods"></index>
			<cart  v-if="index === 3"></cart>
			<user  v-if="index === 4"  @to_goods="to_goods"></user>
		</view>
		<!-- 底部标签栏 -->
		<buttom-tabbar @toIndex="toIndex" ref="btmTab"></buttom-tabbar>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import index from '../index/index.vue'
	import goods from '../goods_index/index.vue'
	import store from '../store_index/seller.vue'
	import cart from '../cart_index/index.vue'
	import user from '../user_index/index.vue'
	export default {
		components: {
			index,
			goods,
			store,
			cart,
			user
		},
		data() {
			return {
				index: 2,                   // tabbar 下标信息
                user: {},                   // 用户信息
                goods: [],                  // 精选好物
				goodsObj: {                 // 请求啊api接口
					page: 1,
					pagesize: 10,
					category_id: 0,
					orderby_type: "number",
					orderby: "desc",
					member_id: 1,
					is_group: '',
					lat: 24.881627,
					lng: 118.628881,
				},
                is_init: 0,                 // 初始次数
			}
		},		
		computed: {
			...mapState({
				systemInfo: state => state.init.systemInfo,     // 设备信息
				long: state => state.init.long,                 // 距离顶部距离
				tabbarActive: state => state.init.tabbarActive ,// tab组件下标
				cart_group: state => state.cart.cart_group,     // 购物车信息
			})
		},
		onShow() {
			// 下标不为空
			this.index = this.tabbarActive
			this.$refs.btmTab._data.active = this.index
		},
        onLoad() {
			// 购物车列表
			this.$store.dispatch('cart/get_cart_group')
            if (this.is_init == 0) {
                // 初始化信息
                this.$api('init').then(res => {
                   let data = res.data;
                   this.$store.dispatch('user/get_pointsRuleS', res.data.pointsRule);
                   this.$store.dispatch('user/get_user', res.data.memberInfo);
                   this.$store.dispatch('address/get_PlaceS', res.data.address)
                   this.$store.dispatch('init/get_base', res.data.ad);
                   this.$store.dispatch('init/get_fun', res.data.func);
                   this.$store.dispatch('sellers/get_shopCategoryS', res.data.shopCategory);
                   this.$store.dispatch('goods/get_categoryS', res.data.giftCategory);
                   this.is_init++;
                })
                // 获取剩余积分
                this.$store.dispatch('init/get_Points');
                // 获取商家公告
                this.$store.dispatch('sellers/get_notice',{ "page": 1,"pagesize": 10 });
				 // 获取商品
				 this.$store.dispatch('init/get_goods', this.goodsObj);
				 // 获取商品
				 this.$store.dispatch('init/get_limitFits', {
					page: 1,
					pagesize: 10,
					category_id: 0,
					orderby_type: "number",
					orderby: "desc",
					member_id: 1,
					is_group: 1,
					lat: 24.881627,
					lng: 118.628881,
				});
				 // 获取商家列表
				 this.shop();
				 // 地理位置授权
				 this.getPlace();
				 // 获取新用户福利信息
				 this.$store.dispatch('init/get_newfit', { is_new_user: 1 })
				 // 获取限时福利A
				 this.$store.dispatch('init/get_LimitACoupons', { is_new_user: 0 });
            }
        },
		onPageScroll(e) {
			// 获取滚动页面位置
			this.$store.dispatch('init/get_scrollTop', e.scrollTop);
		},
		methods: {
			// 新用户优惠信息
			getNewFit() {
				this.$api('baselistBankCoupon', {
					is_new_user: 1
				}).then(res => {
					res.data.forEach(item => {
						item.use_end_time = this.$tools.time_data(item.use_end_time, 'f_date')
						item.use_start_time = this.$tools.time_data(item.use_start_time, 'f_date')
						item.type = 0;
					})
					this.newCoupons =  res.data
					this.$store.dispatch('init/get_newfit', res.data)
				})
			},
			// 获取限时福利
			getlimitA() {
				this.$api('baselistBankCoupon', {
					is_new_user: 0
				}).then(res => {
					res.data.forEach(item => {
						item.use_end_time = this.$tools.time_data(item.use_end_time, 'f_date')
						item.use_start_time = this.$tools.time_data(item.use_start_time, 'f_date')
						item.type = 0;
					})
					this.LimitACoupons = res.data
				    this.$store.dispatch('init/get_LimitACoupons', res.data);
				})
			},
			// 地理位置授权
			getPlace() {
				let that = this;
				//检测用户是否授权
				uni.authorize({
					scope: 'scope.userLocation',
					success(res){
						//获取经纬度坐标
						uni.getLocation({
							type: 'wgs84',
							success: function (res) {
								let obj = {};
								obj.longitude = res.longitude
								obj.latitude = res.latitude
								that.$store.dispatch('user/get_Location',obj)
							},
						});
					}
				})
			},
			// 获取商家列表
			shop() {
			    this.$store.dispatch('init/get_shoppage', {
			    	page: 1,
			    	pagesize: 10,
			    	category_id: '',
			    	orderby_type: "",
			    	orderby: '',
			    	lat: 24.881627,
			    	lng: 118.628881,
			    	member_id: 1
			    })
			},
            // 猜你喜欢
            get_goods() {
                this.$store.dispatch('goods/get_goods', {
                	page: 1,
                	pagesize: 10,
                	category_id: 0,
                	orderby_type: 'number',
                	orderby: 'desc',
                	member_id: 1
                })
            },
			// 页面跳转
			to_goods() {
				this.index = 0
				this.$refs.btmTab._data.active = this.index
			},
			// 回到顶部
			toIndex(index) {
				uni.pageScrollTo({
					scrollTop: 0 ,// 位置
					success: (res) => {
						this.index = index
					}
				})
			},
		},
		
	}
</script>

<style lang="scss" scoped>
	.index-body {
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
	}
	
	.content {
		padding-bottom: 100rpx;
	}
	
	.index-body::-webkit-scrollbar {
		display: none !important;
	}
	
	.content::-webkit-scrollbar {
		display: none !important;
	}
	
	@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
		.content {
			top: $stairTopTow;
			bottom: $navHeight + $navBoxHeight;
		}
	
		/* #ifdef H5 */
		.content {
			top: $barHeight;
			bottom: $navHeight;
		}
		/* #endif */
	}
	
	/*苹果xs适配 H5APP*/
	@media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3) {
		.content {
			top: $stairTopTow;
			bottom: $navHeight + $navBoxHeight;
		}
		
	
		/* #ifdef H5 */
		.content {
			top: $barHeight;
			bottom: $navHeight;
		}
	
	
		/* #endif */
	}
	
	
	/*苹果xr适配 H5APP*/
	@media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 2) {
	
		.content {
			top: $stairTopTow;
			bottom: $navHeight + $navBoxHeight;
		}
	
		/* #ifdef H5 */
		.content {
			top: $barHeight;
			bottom: $navHeight;
			
		}
	
		/* #endif */
	}

</style>
